﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductionManagement.Application.Command.FeedingCommand;
using ProductionManagement.Application.Command.SendWorkersForQualityInspectionCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Domain.Dtos.SendWorkersForQualityInspectionDTOS;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProductionManagement.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class SWFQIController : ControllerBase
    {
        public IMediator mediator;

        public SWFQIController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加生产派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddSendWorkersForQualityInspection(SendWorkersForQualityInspectionCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.dtos.Office_Id = sid;
                command.dtos.User_Id = int.Parse(userId);
                command.dtos.AddName = name;
                command.dtos.AddTime = DateTime.Now;
                command.dtos.IsDel = false;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 列表显示生产派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SendWorkersForQualityInspectionSelectDtos>> SelectSendWorkersForQualityInspection([FromQuery] SendWorkersForQualityInspectionSelectCommand command)
        {
            ApiResult<SendWorkersForQualityInspectionSelectDtos> result = new ApiResult<SendWorkersForQualityInspectionSelectDtos>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 删除生产派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteSendWorkersForQualityInspection(SendWorkersForQualityInspectionDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.Dname = name;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填生产派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SendWorkersForQualityInspectionSetDtos>> SetSendWorkersForQualityInspection([FromQuery]SendWorkersForQualityInspectionSetCommand command)
        {
            ApiResult<SendWorkersForQualityInspectionSetDtos> result = new ApiResult<SendWorkersForQualityInspectionSetDtos>();
            try
            {

                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改生产派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateSendWorkersForQualityInspection(SendWorkersForQualityInspectionUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.dtos.UpdName = name;
                command.dtos.UpdTime = DateTime.Now;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
    }
}
